/**
 * 充值包管理API测试脚本
 * 用于测试后端API接口是否正常工作
 */

const cloud = require('wx-server-sdk');

// 初始化云开发
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

// 测试数据
const testPackageData = {
  name: '测试充值包',
  amount: 10000, // 100元，以分为单位
  bonus: 1000,   // 赠送10元
  isRecommended: true,
  label: '测试',
  description: '这是一个测试充值包'
};

async function testRechargePackageAPI() {
  console.log('开始测试充值包管理API...');
  
  try {
    // 1. 测试获取充值包列表
    console.log('\n1. 测试获取充值包列表...');
    const listResult = await cloud.callFunction({
      name: 'cloud',
      data: {
        action: 'admin/recharge_package/list'
      }
    });
    console.log('获取列表结果:', JSON.stringify(listResult.result, null, 2));
    
    // 2. 测试添加充值包
    console.log('\n2. 测试添加充值包...');
    const addResult = await cloud.callFunction({
      name: 'cloud',
      data: {
        action: 'admin/recharge_package/add',
        ...testPackageData
      }
    });
    console.log('添加结果:', JSON.stringify(addResult.result, null, 2));
    
    if (addResult.result.code === 0) {
      const packageId = addResult.result.data._id;
      console.log('新添加的充值包ID:', packageId);
      
      // 3. 测试编辑充值包
      console.log('\n3. 测试编辑充值包...');
      const editData = {
        ...testPackageData,
        name: '测试充值包(已编辑)',
        amount: 20000,
        bonus: 3000
      };
      
      const editResult = await cloud.callFunction({
        name: 'cloud',
        data: {
          action: 'admin/recharge_package/edit',
          packageId: packageId,
          ...editData
        }
      });
      console.log('编辑结果:', JSON.stringify(editResult.result, null, 2));
      
      // 4. 测试切换状态
      console.log('\n4. 测试切换状态...');
      const toggleResult = await cloud.callFunction({
        name: 'cloud',
        data: {
          action: 'admin/recharge_package/toggle_status',
          packageId: packageId
        }
      });
      console.log('切换状态结果:', JSON.stringify(toggleResult.result, null, 2));
      
      // 5. 测试删除充值包
      console.log('\n5. 测试删除充值包...');
      const deleteResult = await cloud.callFunction({
        name: 'cloud',
        data: {
          action: 'admin/recharge_package/delete',
          packageId: packageId
        }
      });
      console.log('删除结果:', JSON.stringify(deleteResult.result, null, 2));
    }
    
    console.log('\n✅ 所有API测试完成!');
    
  } catch (error) {
    console.error('❌ 测试过程中出现错误:', error);
  }
}

// 运行测试
testRechargePackageAPI();

module.exports = {
  testRechargePackageAPI
};